package com.icinfo.cloud.provider.punish.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.icinfo.cloud.common.core.page.PageResponse;
import com.icinfo.cloud.provider.common.constant.Constant;
import com.icinfo.cloud.provider.common.constant.PunishMainServicePortUriConstant;
import com.icinfo.cloud.provider.exception.BusinessException;
import com.icinfo.cloud.provider.punish.csource.manage.model.CaseSourceRegisterMainInfo;
import com.icinfo.cloud.provider.punish.csource.manage.vo.CaseSourcePartyInfoVo;
import com.icinfo.cloud.provider.punish.mapper.CaseSourceRegisterMainInfoMapper;
import com.icinfo.cloud.provider.punish.service.ICaseSourceRegisterMainInfoService;
import com.icinfo.cloud.provider.punish.vo.AssociatedTaskVo;
import com.icinfo.cloud.xzcf.provider.member.userinfo.dto.UserDTO;
import com.icinfo.framework.common.web.Result;
import com.icinfo.framework.mybatis.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Service
public class CaseSourceRegisterMainInfoServiceImpl implements ICaseSourceRegisterMainInfoService {

    @Resource
    private CaseSourceRegisterMainInfoMapper caseSourceRegisterMainInfoMapper;
    @Value("${tzService.url}")
    private String url;

    /**
     * 我的案源列表
     * @param page 页码
     * @param size 容量
     * @param partyNameOrCaseReason 当事人名称或案源名称
     * @param caseSourceStatus 案源状态
     * @return
     */
    @Override
    public PageResponse<AssociatedTaskVo> myCaseSourceList(int page, int size, String partyNameOrCaseReason, String caseSourceStatus) {
        UserDTO user = getUser();
        PageHelper.startPage(page, size);
        List<AssociatedTaskVo> caseSourcePageNewVos = caseSourceRegisterMainInfoMapper.caseSourceList(String.valueOf(user.getUserId()), Constant.STRING_3, partyNameOrCaseReason);
        return new PageResponse<>(caseSourcePageNewVos);
    }

    @Override
    public List<CaseSourcePartyInfoVo> getCaseSourcePartyInfo(String caseSourceId) {
        String response = HttpUtil.createGet(url.concat("/punish/csource/manage/party/getCaseSourcePartyInfo" + "?caseSourceId=" + caseSourceId)).execute().body();
        JSONObject object = JSONObject.parseObject(response);
        if (!Constant.STRING_200.equals(object.getString("code"))) {
            throw new BusinessException("获取案源当事人失败！");
        }
        return JSON.parseArray(object.getString("data"), CaseSourcePartyInfoVo.class);
    }

    @Override
    public CaseSourceRegisterMainInfo getCaseSourceRegisterMainInfoById(String id) {
        if (StrUtil.isBlank(id)){
            return null;
        }
        return caseSourceRegisterMainInfoMapper.selectByPrimaryKey(id);
    }
}
